/*
 * @FileName: 
 * @Descripttion: 由于本实验无执行器件
 * @Author: jiangfeng.zhang(jiangfeng.zhang@aliyun.com)
 * @LastEditTime: 2019-11-13 11:27:15
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>

float fPrencetValue = 50; //初始化当前值为50

struct ST_PID_CORE
{
    float setpoint;  //设定值
    float Kp;        //比例系数
    float Kd;        //积分系数
    float Ki;        //微分系数
    float integral;  //积分值
    float lasterror; //前一拍偏差
    float result;    //输出值
};

struct ST_PID_CORE gst_TestPidCore =
    {
        100, //设定值
        0.4,
        0.2,
        0.2,
        1,
        1,
        1,
};

//入口参数pid指针，采集器当前的数据
void PidControl(struct ST_PID_CORE *pstPidCore, float fPv)
{
    float thisError;
    thisError = pstPidCore->setpoint - fPv; //当前误差等于设定值减去当前值
    pstPidCore->lasterror = thisError;      //更新当前误差
    pstPidCore->integral += thisError;      //误差积分，把所有误差累加起来
    pstPidCore->result = pstPidCore->Kp * thisError + pstPidCore->Ki * pstPidCore->integral + pstPidCore->Kd * (thisError - pstPidCore->lasterror);
}
void main()
{
    for (;;)
    {
        PidControl(&gst_TestPidCore, fPrencetValue);
        fPrencetValue = gst_TestPidCore.result * 1.0; //这里模拟执行部件导致的结论是如此
        printf("%f\n", gst_TestPidCore.result);       //pid控制算法的out值。
        sleep(1);
    }
}
